package com.itheima.service;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * service完成真正的业务逻辑
 */
@Service  //将UserService交给spring管理
@Slf4j
public class UserService {

    /*spring会自动注入对象*/
    @Autowired
    private UserMapper userMapper;

    public User selectById(Integer id) {

        //直接使用mapper即可
        User user = userMapper.selectById(id);
        log.info("读取到id为{}的数据：{}",id,user);
        System.out.println(user);
        return user;
    }

    public List<User> selectOrderByAge() {
        List<User> users = userMapper.selectOrderByAge("age");
        return users;
    }

    //根据条件完成查询
    public List<User> selectByCondition(User conditionUser) {

        List<User> list = userMapper.selectByCondition(conditionUser);
        return list;
    }

    public void addUser(User user) {

        userMapper.addUser(user);
    }

    //删除方法
    public boolean delUserById(Integer delId) {

        //调用mapper删除，返回删除影响到的行数
        int count = userMapper.delUserById(delId);
        System.out.println("影响到的行数："+count);
        //根据影响行数，判断是否删除成功
        if(count == 0) {
            return false;
        }else {
            return true;
        }
    }
}
